package com.drojian.demo1.test4;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.drojian.demo.R;
import com.drojian.demo1.adapter.MyRecyclerViewAdapter;
import com.drojian.demo1.pojo.Fruit;

import java.util.ArrayList;

public class RecyclerViewActivity extends AppCompatActivity implements View.OnClickListener {
    private Button recycler_add;
    private Button recycler_delete;
    private Button recycler_list;
    private Button recycler_grid;
    private Button recycler_flow;
    private RecyclerView recyclerView;
    private ArrayList<Fruit> datas = new ArrayList<>();
    private MyRecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.ui4_activity_recycler_view);
        setContentView(R.layout.ui4_activity_recycler_view);

        initView();
        //准备数据集合
        initData();
        //设置recyclerView适配器
        adapter = new MyRecyclerViewAdapter(RecyclerViewActivity.this,datas);
        recyclerView.setAdapter(adapter);
        //layoutManager
        recyclerView.setLayoutManager(new LinearLayoutManager(RecyclerViewActivity.this,LinearLayoutManager.VERTICAL,false));
        //添加分割线
        recyclerView.addItemDecoration(new DividerItemDecoration( RecyclerViewActivity.this,DividerItemDecoration.VERTICAL));

    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.back){
            finish();
        }else if(v.getId()==R.id.recycler_add){
            Fruit fruit = new Fruit("按钮", R.drawable.anniu);
            adapter.addData(1,fruit);
        }else if(v.getId()==R.id.recycler_delete){
            adapter.removeData(1);
        }else if(v.getId()==R.id.recycler_list){
            recyclerView.setLayoutManager(new LinearLayoutManager(RecyclerViewActivity.this,LinearLayoutManager.VERTICAL,false));
        }else if(v.getId()==R.id.recycler_grid){
            recyclerView.setLayoutManager(new GridLayoutManager(RecyclerViewActivity.this,3, GridLayoutManager.VERTICAL,false));

        }else if(v.getId()==R.id.recycler_flow){
            recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
        }

    }

    public void initView(){
        Button back = findViewById(R.id.back);
        recycler_add=findViewById(R.id.recycler_add);
        recycler_delete=findViewById(R.id.recycler_delete);
        recycler_list=findViewById(R.id.recycler_list);
        recycler_grid=findViewById(R.id.recycler_grid);
        recycler_flow=findViewById(R.id.recycler_flow);
        recyclerView=findViewById(R.id.recyclerView);

        back.setOnClickListener(this);
        recycler_add.setOnClickListener(this);
        recycler_delete.setOnClickListener(this);
        recycler_list.setOnClickListener(this);
        recycler_grid.setOnClickListener(this);
        recycler_flow.setOnClickListener(this);
        recyclerView.setOnClickListener(this);
    }


    private void initData() {
        for(int i = 0; i<8 ;i++){
            Fruit apple = new Fruit("apple",R.drawable.apple);
            datas.add(apple);
            Fruit banana = new Fruit("banana",R.drawable.banana);
            datas.add(banana);
            Fruit orange = new Fruit("orange",R.drawable.orange);
            datas.add(orange);
            Fruit watermelon = new Fruit("watermelon",R.drawable.watermelon);
            datas.add(watermelon);
            Fruit pear = new Fruit("pear",R.drawable.pear);
            datas.add(pear);
            Fruit pineapple = new Fruit("pineapple",R.drawable.pineapple);
            datas.add(pineapple);
            Fruit strawberry = new Fruit("strawberry",R.drawable.strawberry);
            datas.add(strawberry);
            Fruit cherry = new Fruit("cherry",R.drawable.cherry);
            datas.add(cherry);
            Fruit mango = new Fruit("mango",R.drawable.mango);
            datas.add(mango);

        }

    }

}